<?php

/**
 * IndexController - Index
 *
 * @author Fernando Dias Oliveira <rodox17@gmail.com>
 * @package intranet
 * @subpackage controller
 */

class ProductController extends AppBaseController 
{
	public function init()
	{
		parent::init();		
		
		$this->_helper->layout->disableLayout();
		
		error_reporting(0);
	}
	
	public function ingredientesAction()
	{
		$caf09 = new caf09;
		
		$arr = $caf09->getAllByCod($this->_request->getPost("itens"), $this->_request->getPost("ped"));

		$this->view->arr_ingred = $arr;
	}
	
	public function comentarioAction()
	{
		$cod = $this->_request->getPost("cod");
		$cod_item = $this->_request->getPost("cod_item");
		$arr_cod = array();
		
		$caf04 = new caf04();
		
		$arr = $caf04->getItensById($cod, $cod_item);
		
		foreach ($arr as $key => $val)
		{
			$arr_cod[] = $val["caf04_cod"];
		}
		
		if(!$arr)
		{
			$arr = "";
		}
		
		$this->view->arr_coment = $arr;
		$this->view->arr_cod    = $arr_cod;
	}
	
	public function descontoAction()
	{
		$cod = $this->_request->getPost("cod");
		$cod_item = $this->_request->getPost("cod_item");
		$arr_cod = array();
		
		$caf04 = new caf04();
		$caf02 = new caf02();
		
		$objCaf02 = $caf02->getDesconto($cod_item);
		
		$arr = $caf04->getItensById($cod, $cod_item);
		
		foreach ($arr as $key => $val)
		{
			$arr_cod[] = $val["caf04_cod"];
		}
		
		if(!$objCaf02->caf02_desconto)
		{
			$desconto = 0;
		}
		else
		{
			$desconto = $objCaf02->caf02_desconto;
		}
		
		$this->view->arr_coment = $arr;
		$this->view->arr_cod    = $arr_cod;
		$this->view->desconto   = $desconto;
	}
	
	public function comentarioCOPYAction()
	{
		$cod = $this->_request->getPost("cod");
		$cod_item = $this->_request->getPost("cod_item");
		$arr_cod = array();
		
		$caf04 = new caf04();
		
		$arr = $caf04->getItensById($cod, $cod_item);
		
		foreach ($arr as $key => $val)
		{
			$arr_cod[] = $val["caf04_cod"];
		}
		
		$this->view->arr_coment = $arr;
		$this->view->arr_cod    = $arr_cod;
	}

    public function indexAction() 
    {


    }
    
    public function subproductsAction()
    {
    	$caf05 = new caf05();
    	
    	$arr = array();
    	$total = array();
    	$cod = 0;
    	$prod = "";
    	$pr = 1;
    	
    	foreach ($caf05->getProductByMenu($_POST["cod_menu"], $_POST["cod"]) as $key => $val)
    	{
    		if($prod == $val["caf04_cod_prod"] && $val["caf04_cod_prod"])
    		{
    			if(!$total[$cod - $pr])
    			{
    				$total[$cod - $pr] = 1;
    			}
    			
  				$total[$cod - $pr] += 1;
  				
				$val["caf04_qtd"] = $total[$cod - $pr];
				
				$arr[$cod - $pr] = $val;
    			
				$pr++;
				
    		}
    		else
    		{
    			$arr[$cod] = $val;
    			
    			$pr = 1;
    		}
    		
    		$cod++;
    		
    		$prod = $val["caf04_cod_prod"];
    	}
    	
    	$this->view->arr_prod = $arr;
    	$this->view->edit     = $this->_request->getPost("pedido");
    	$this->view->gerente  = $this->_request->getPost("gerente");
    	
    }
    
    public function listproducts()
    {
    	
    }
    
    public function newAction()
    {
 		$caf03 = new caf03();
		
		$arr = $caf03->getArray();
		
		$this->view->arr_menu = $arr;
    }
    
    public function listAction()
    {
    	
    }
    
    public function saveingredAction()
    {
    	$caf08 = new caf08();
    	
    	$chk = $this->_request->getPost("chk");
    	
    	$chk = split(";", substr($chk, 0, strlen($chk) - 1));
    	
    	$caf08->delete("caf08_cod_ped = {$this->_request->getPost("ped")}");
    	
    	foreach ($chk as $key => $val)
    	{
    		$valid = $caf08->fetchAll("caf08_cod_ped = {$this->_request->getPost("ped")} AND caf08_cod_ing = $val");
    		
    		if(!$val)
				continue;
			
			if(!$valid)
    			$caf08->salvar(array("caf08_cod_ped" => $this->_request->getPost("ped"), "caf08_cod_ing" => $val));
    	}
    	
    	exit;
    }
    
    function verificarProdutoAction()
    {
		error_reporting(1);
    	$caf05 = new caf05();
    	
    	$id = $this->_request->getParam("id");
    	
    	$Array = $caf05->find($id)->current();
    	
    	if($Array)
    	{
    		die(json_encode($Array->toArray()));
    	}else{
    		die(json_encode(array(0)));
    	}
    }
    
    public function saveAction()
    {
    	$cab02 = new caf02();
    	
    	$cab02->save($_POST);
    	
    	exit;
    }
}

?>